package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.CollegesDto;
import io.geekidea.boot.test.entity.Colleges;
import io.geekidea.boot.test.mapper.CollegesMapper;
import io.geekidea.boot.test.query.CollegesQuery;
import io.geekidea.boot.test.service.CollegesService;
import io.geekidea.boot.test.vo.CollegesVo;
import io.geekidea.boot.test.query.AppCollegesQuery;
import io.geekidea.boot.test.vo.AppCollegesVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2025-02-19
 */
@Slf4j
@Service
public class CollegesServiceImpl extends ServiceImpl<CollegesMapper, Colleges> implements CollegesService {

    @Autowired
    private CollegesMapper collegesMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addColleges(CollegesDto dto) {
        Colleges colleges = new Colleges();
        BeanUtils.copyProperties(dto, colleges);
        return save(colleges);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateColleges(CollegesDto dto) {
        Long id = dto.getId();
        Colleges colleges = getById(id);
        if (colleges == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, colleges);
        return updateById(colleges);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteColleges(Long id) {
        return removeById(id);
    }

    @Override
    public CollegesVo getCollegesById(Long id) {
        return collegesMapper.getCollegesById(id);
    }

    @Override
    public Paging<CollegesVo> getCollegesPage(CollegesQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<CollegesVo> list = collegesMapper.getCollegesPage(query);
        Paging<CollegesVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppCollegesVo getAppCollegesById(Long id) {
        return collegesMapper.getAppCollegesById(id);
    }

    @Override
    public Paging<AppCollegesVo> getAppCollegesPage(AppCollegesQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppCollegesVo> list = collegesMapper.getAppCollegesPage(query);
        Paging<AppCollegesVo> paging = new Paging<>(list);
        return paging;
    }

}
